package com.example.amoocspringboot.service;

import com.example.amoocspringboot.entity.User;
import com.example.amoocspringboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserService {

    private final UserMapper userMapper;
    private final PasswordEncoder passwordEncoder;

    @Autowired
    public UserService(UserMapper userMapper, PasswordEncoder passwordEncoder) {
        this.userMapper = userMapper;
        this.passwordEncoder = passwordEncoder;
    }

    // 注册用户
    public int register(User user) {
        // 加密密码
        user.setPassword(passwordEncoder.encode(user.getPassword()));

        // 设置默认状态（激活）
        if(user.getStatus() == null) {
            user.setStatus(1);
        }

        // 设置创建时间
        if(user.getCreateTime() == null) {
            user.setCreateTime(new Date());
        }

        return userMapper.insert(user);
    }

    // 更新用户信息（可选）
    public int updateUser(User user) {
        // 更新时保持密码不变或单独处理密码更新
        return userMapper.updateById(user);
    }
}